﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Bazookabird.Common.Plugin;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Bazookabird.Wpf.Queries
{
    public class QueryListViewModel : PanelViewModelBase
    {
        private readonly IShell _shell;

        public QueryListViewModel(IShell shell, TfsTeamProjectCollection projectCollection, string projectName, IEnumerable<IQueryHandler> queryHandlers)
        {
            _shell = shell;
            QueryHandlers = queryHandlers;
            ProjectCollection = projectCollection;
            ProjectName = projectName;
        }

        private QueryHierarchy _queryHierarchy;
        public QueryHierarchy QueryHierarchy
        {
            get { return _queryHierarchy; }
            set 
            {
                _queryHierarchy = value;
                NotifyOfPropertyChange(() => QueryHierarchy);
            }
        }

        private QueryDefinition _selectedQueryDefinition;
        public QueryDefinition SelectedQueryDefinition
        {
            get { return _selectedQueryDefinition; }
            set
            {
                _selectedQueryDefinition = value;
                NotifyOfPropertyChange(() => SelectedQueryDefinition);
                NotifyOfPropertyChange(() => CanAddQueryHandler);
            }
        }

        private IQueryHandler _selectedQueryHandler;
        public IQueryHandler SelectedQueryHandler
        {
            get { return _selectedQueryHandler; }
            set 
            { 
                _selectedQueryHandler = value;
                NotifyOfPropertyChange(() => SelectedQueryHandler);
                NotifyOfPropertyChange(() => CanAddQueryHandler);
            }
        }

        private IEnumerable<IQueryHandler> _queryHandlers;
        public IEnumerable<IQueryHandler> QueryHandlers
        {
            get { return _queryHandlers; }
            set 
            { 
                _queryHandlers = value; 
                NotifyOfPropertyChange(() => QueryHandlers);
            }
        }

        private string _projectName;
        public string ProjectName
        {
            get { return _projectName; }
            set
            {
                _projectName = value;
                NotifyOfPropertyChange(() => ProjectName);
            }
        }

        private TfsTeamProjectCollection _projectCollection;
        public TfsTeamProjectCollection ProjectCollection
        {
            get { return _projectCollection; }
            set
            {
                _projectCollection = value;
                NotifyOfPropertyChange(() => ProjectCollection);
            }
        }

        private ContextMenu _contextMenu;
        public ContextMenu ContextMenu
        {
            get { return _contextMenu; }
            set
            {
                _contextMenu = value;
                NotifyOfPropertyChange(() => ContextMenu);
            }
        }

        private WorkItemStore _workItemStore;
        public WorkItemStore WorkItemStore
        {
            get { return _workItemStore; }
            set
            {
                _workItemStore = value;
                NotifyOfPropertyChange(() => WorkItemStore);
            }
        }

        public void AddQueryHandler()
        {
            SelectedQueryHandler.WorkItemStore = WorkItemStore;
            SelectedQueryHandler.QueryDefinition = SelectedQueryDefinition;

            _shell.AddPluginToCurrentScreen(SelectedQueryHandler);
        }

        public bool CanAddQueryHandler
        {
            get { return SelectedQueryDefinition != null && SelectedQueryHandler != null; }
        }

        public void TreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            SelectedQueryDefinition = e.NewValue as QueryDefinition;
        }

        protected override void OnActivate()
        {
            base.OnActivate();
            DisplayName = "List of Queries";

            Refresh();
        }

        private new void Refresh()
        {
            WorkItemStore = ProjectCollection.GetService<WorkItemStore>();
            QueryHierarchy = WorkItemStore.Projects[ProjectName].QueryHierarchy;
            QueryHierarchy.Refresh();

            base.Refresh();
        }
    }
}
